package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.dao.impl.NewsImpl;
import com.cn.wanxi.dao.impl.ProductImpl;
import com.cn.wanxi.dto.HomeModelDto;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IHomeService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class HomeServiceImpl implements IHomeService {
    /***
     * 增加公司信息
     * @param companyModel 传递公司的实体类
     * @return
     */
    @Override
    public int add(CompanyModel companyModel) {
        return 0;
    }

    /**
     * 封装home信息
     * 1.封装公司信息
     * 2.封装新闻信息
     * 3.封装产品信息
     *
     * @return
     */
    @Override
    public HomeModelDto getHomeModelDto() {
        HomeModelDto homeModelDto = new HomeModelDto();
        homeModelDto.setNewsList(getNewsList());
        homeModelDto.setProductList(getProductList());

        return homeModelDto;
    }

    private List<NewsModel> getNewsList() {
        NewsDao newsDao = new NewsImpl();
        List<NewsModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();

        Long length = jedis.llen("newsId");
        if (length > 0) {
            List<String> ids = jedis.lrange("newsId", 0, -1);
            List<String> titles = jedis.lrange("newsTitle", 0, -1);
            List<String> contents = jedis.lrange("newsContent", 0, -1);
            for (int i = 0; i < ids.size(); i++) {
                NewsModel newsModel = new NewsModel();
                newsModel.setId(Integer.parseInt(ids.get(i)));
                newsModel.setTitle(titles.get(i));
                newsModel.setContent(contents.get(i));
                list.add(newsModel);
            }
        } else {
            list = newsDao.getHomeNews();
            for (int i = 0; i < list.size(); i++) {
                jedis.rpush("newsId", list.get(i).getId().toString());
                jedis.rpush("newsTitle", list.get(i).getTitle());
                jedis.rpush("newsContent", list.get(i).getContent());
                jedis.expire("newsId", 20);
                jedis.expire("newsTitle", 20);
                jedis.expire("newsContent", 20);
            }
        }
        return list;
    }

    private List<ProductModel> getProductList() {
        ProductDao productDao = new ProductImpl();
        List<ProductModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        Long length = jedis.llen("productId");
        if (length > 0) {
            List<String> ids = jedis.lrange("productId", 0, -1);
            List<String> name = jedis.lrange("productName", 0, -1);
            List<String> contents = jedis.lrange("productContent", 0, -1);
            List<String> imgHref = jedis.lrange("productImgHref", 0, -1);
            List<String> price = jedis.lrange("productPrice", 0, -1);

            for (int i = 0; i < ids.size(); i++) {
                ProductModel productModel = new ProductModel();
                productModel.setId(Integer.parseInt(ids.get(i)));
                productModel.setName(name.get(i));
                productModel.setContent(contents.get(i));
                productModel.setPrice(price.get(i));
                productModel.setImgHref(imgHref.get(i));
                list.add(productModel);
            }
        } else {
            list = productDao.getHomeProductList();
            for (int i = 0; i < list.size(); i++) {
                jedis.lpush("productId", list.get(i).getId().toString());
                jedis.lpush("productName", list.get(i).getName());
                jedis.lpush("productContent", list.get(i).getContent());
                jedis.lpush("productPrice", list.get(i).getPrice());
                jedis.lpush("productImgHref", list.get(i).getImgHref());
            }
        }
        return list;
    }
}
